//
//  FileRepositoryControlCenter.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/6/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "FileRepositoryControlCenter.h"

@implementation FileRepositoryControlCenter

@synthesize imageLibrary;
@synthesize imageQuality;
@synthesize documentDirectory = _documentDirectory;
@synthesize JPGCompressedQuality = _JPGCompressedQuality;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        self.imageQuality = [[NSUserDefaults standardUserDefaults] integerForKey:kSRPreferenceImageQuantityLevel];
        _documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        switch ([[NSUserDefaults standardUserDefaults] integerForKey:kSRPreferenceImageQuantityLevel]) {
            case 0:
                _JPGCompressedQuality = 0.8;
                break;
                
            case 2:
                _JPGCompressedQuality = 1;
                break;
                
            default:
                _JPGCompressedQuality = 0.9;
                break;
        }
        
    }
    
    return self;
}

- (void)dealloc 
{
    [imageLibrary release];
    [documentDirectory release];
    
    [super dealloc];
}

+ (NSString *)saveImage:(UIImage *)image withType:(kSaveImageFileType)imageType toSuffixLibrary:(NSString *)destinationPrefix error:(NSError **)error
{
    NSError *e = nil;

    if (image == nil) {
        NSDictionary *detail = [[NSDictionary alloc] initWithObjectsAndKeys:@"Image data could not be empty", NSLocalizedDescriptionKey, nil];
        
        if (*error != nil) {
            *error = [[NSError alloc] initWithDomain:kSRDefaultErrorDomain code:1000 userInfo:detail];
        }
        
        [detail release];
        
        return nil;
    }
    
    [image retain];
    
    FileRepositoryControlCenter *frcc = [[FileRepositoryControlCenter alloc] init];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd.HHmmss";
    
    NSDateFormatter *defaultPrefix = [[NSDateFormatter alloc] init];
    defaultPrefix.dateFormat = @"yyyy.MM.dd";

    
    NSData *imageDataConverted = nil;
    NSString *imageSuffix = nil;
    
    if (imageType == kSaveImageFileTypeToJPG) {
        imageDataConverted = UIImageJPEGRepresentation(image, frcc.JPGCompressedQuality);
        imageSuffix = @"jpg";
    } else if (imageType == kSaveImageFileTypeToPNG) {
        imageDataConverted = UIImagePNGRepresentation(image);
        imageSuffix = @"png";
    }
    
    if (imageDataConverted != nil) {
        [imageDataConverted retain];
    }
    
    if (destinationPrefix == nil) {
        destinationPrefix = [defaultPrefix stringFromDate:[SLCommonsDateUtility GMTDateToLocalDate:[SLCommonsDateUtility currentGMTDate]]];
    }
    
    NSString *imageLib = [frcc.documentDirectory stringByAppendingPathComponent:destinationPrefix];
    BOOL isDirectory;
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:imageLib isDirectory:&isDirectory]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:imageLib withIntermediateDirectories:YES attributes:nil error:&e];
        if (e != nil) {
            
            if (*error != nil) {
                *error = e;
            }
            
            [frcc release];
            [image release];
            [formatter release];
            [defaultPrefix release];
            [imageDataConverted release];
            
            return nil;
        }
    }
    
    
//    NSString *saveLocation = [[[[frcc.documentDirectory stringByAppendingPathComponent:destinationPrefix] stringByAppendingPathComponent:[formatter stringFromDate:[SLCommonsDateUtility GMTDateToLocalDate:[SLCommonsDateUtility currentGMTDate]]]] stringByAppendingPathExtension:imageSuffix] retain];
    
    NSString *saveLocation = [imageLib stringByAppendingFormat:@"/%@.%@",
                                                              [formatter stringFromDate:[SLCommonsDateUtility GMTDateToLocalDate:[SLCommonsDateUtility currentGMTDate]]],
                                                              imageSuffix];
    
    [formatter release];
    [defaultPrefix release];
    [frcc release];
    
    [imageDataConverted writeToFile:saveLocation options:NSDataWritingFileProtectionComplete error:&e];
    
    if (e != nil) {
        
        if (*error != nil) {
            *error = e;
        }
        
        return nil;
    }
    
    [imageDataConverted release];
    
    return saveLocation;
}

+ (UIImage *)readImageWithIamgename:(NSString *)imageName error:(NSError **)error
{
    UIImage *imageCanReaded = nil;
    
    return imageCanReaded;
}

@end
